﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "ClipBoardElementChangeHandler.h"
#include "IDocument.h"
#include "IElement.h"
#include "ClipBoard.h"
#include "IElementsChangeEventArgs.h"
#include "IElementsChangeEvent.h"
#include <algorithm>
#include <iterator>
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;
using namespace std;

ClipBoardElementChangeHandler::ClipBoardElementChangeHandler(ClipBoard* pClipBoard)
    :m_pClipBoard(pClipBoard)
{
}

void ClipBoardElementChangeHandler::On(IElementsChangeEventArgs* pArgs)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pArgs, L"pArgs为空",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_VOID_UNLESS(m_pClipBoard, L"pArgs为空",L"GDMPLab",L"2024-03-30");

    const IElementsChangeEvent* pEvent = dynamic_cast<const IElementsChangeEvent*>(pArgs->GetEvent());
    DBG_WARN_AND_RETURN_VOID_UNLESS(pEvent, L"pEvent为空",L"GDMPLab",L"2024-03-30");

    IDocument* pDoc = (IDocument*)pEvent->GetSender();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");

    // 如果发生element变化的文档不是剪贴板当前的源文档，忽略
    if (pDoc != m_pClipBoard->GetFromDocument())
        return;

    const std::vector<const IElement*>& pElements = pArgs->GetElements();
    if (ElementsChangeEventType::ElementsPreDeletionEvent == pEvent->GetElementsChangeEventType() && !pElements.empty())
    {
        // 收集被删除的element ids
        std::vector<ElementId> elemIdsDeleted;
        elemIdsDeleted.resize(pElements.size());
        FOR_EACH_INDEX(idx, pElements)
        {
            elemIdsDeleted[idx] = pElements[idx]->GetElementId();
        }

        if (m_pClipBoard->HasContent())
        {
            // 这里的具体行为由业务定
            const std::set<ElementId>& copiedElementIds = m_pClipBoard->GetCopyedElementIds();
            std::set<ElementId> result;
            std::set_intersection(copiedElementIds.begin(), copiedElementIds.end(), elemIdsDeleted.begin(), elemIdsDeleted.end(), std::inserter(result, result.end()));
            if (result.size() > 0)
            {
                m_pClipBoard->ClearContent();
            }
        }
    }
}
